﻿using AutoMapper;
using MediatR;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;
using Yitter.IdGenerator;

namespace U.Enterprise.Api.Applications.Scalesetting
{
    /// <summary>
    /// 活出称重添加
    /// </summary>
    public class LiveAnimalWeighingCreateHander : IRequestHandler<LiveAnimalWeighingCreateCommand, Resqults>
    {
        private readonly IMapper mapper;
        private readonly IBateRepository<LiveAnimalweighing> liveanimal;

        public LiveAnimalWeighingCreateHander(IMapper mapper, IBateRepository<LiveAnimalweighing> liveanimal)
        {
            this.mapper = mapper;
            this.liveanimal = liveanimal;
        }

        public async Task<Resqults> Handle(LiveAnimalWeighingCreateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults res = new Resqults() { Code=0};
                LiveAnimalweighing live = mapper.Map<LiveAnimalweighing>(request);
                live.LiveAnimalweighingNo = YitIdHelper.NextId().ToString();
                res.Code = await liveanimal.Add(live);
                if (res.Code > 0)
                {
                    res.Messges = "添加成功";
                }
                return res;
            }
            catch (Exception)
            {   

                throw;
            }
        }
    }
}
